package com.glory.gloryquickui.view

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.TextView
import com.glory.gloryquickui.R
import com.glory.gloryquickui.entity.FieldBean
import com.glory.gloryquickui.viewmodel.QuickUIViewModel
import com.glory.gloryquickui.widget.edit.PasswordEditText

/**
 *密码输入框
 */
class PasswordEditTextController(context: Context?,val viewModel: QuickUIViewModel) : ViewController<FieldBean?>(context) {
    private var tvPasswordEditName: TextView? = null
    private var tvPasswordEditValue: PasswordEditText? = null
    override fun resLayoutId(): Int {
        return R.layout.layout_password_edittext
    }

    override fun onBindView(data: FieldBean?) {
        tvPasswordEditName!!.text = data?.config?.label
        tvPasswordEditValue!!.hint = data?.placeholder

        //设置可禁止
        tvPasswordEditValue?.isEnabled = !data?.disabled!!
        //设置最大字符
        tvPasswordEditValue?.setMaxLength(data.maxlength.toString())
        //设置前后图标
        tvPasswordEditValue?.setLeftAndRightIcon(data.prefixIcon,data.suffixIcon)

        tvPasswordEditValue?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                if (!data.readonly) {
                    val map = viewModel.pageData.value!!.toMutableMap()
                    map[data.vModel] = p0.toString()
                    viewModel.pageData.value = map
                }
            }

            override fun afterTextChanged(p0: Editable?) {

            }
        })
    }


    override fun onCreatedView(view: View?) {
        tvPasswordEditName = view?.findViewById(R.id.tvPasswordEditName)
        tvPasswordEditValue = view?.findViewById(R.id.tvPasswordEditValue)
    }

    override fun masterPart(): View? {
        return tvPasswordEditValue
    }
}